//
//  BaseNavigationController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/7.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class BaseNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 15.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.titleTextAttributes = [NSAttributedString.Key.font: UIFont.medium(18),
                                              NSAttributedString.Key.foregroundColor: UIColor.textDark]
            appearance.backgroundColor = .white
            appearance.shadowColor = .clear
            UINavigationBar.appearance().standardAppearance = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
        } else {
            UINavigationBar.appearance().titleTextAttributes =
                [NSAttributedString.Key.font: UIFont.medium(18),
                 NSAttributedString.Key.foregroundColor: UIColor.textDark]
            UINavigationBar.appearance().tintColor = UIColor.textDark
            UINavigationBar.appearance().barTintColor = UIColor.white
            UINavigationBar.appearance().shadowImage = UIImage()
        }
        
        if interactivePopGestureRecognizer != nil {
            interactivePopGestureRecognizer?.delegate = self
        }
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if children.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            let backItem = UIBarButtonItem(imageName: "nav_back", target: self, action: #selector(back))
            if UIDevice.avaiableSystem < 11 {
                let spaceItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
                spaceItem.width = -10.0
                viewController.navigationItem.leftBarButtonItems = [spaceItem, backItem]
            } else {
                viewController.navigationItem.leftBarButtonItem = backItem
            }
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func back() {
        popViewController(animated: true)
    }
}

extension BaseNavigationController: UIGestureRecognizerDelegate {
    /// 开始接收到手势的代理方法
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == interactivePopGestureRecognizer {
            // 禁用根控制器响应手势
            if viewControllers.count < 2 || visibleViewController == viewControllers[0] {
                return false
            }
        }
        return true
    }
    
    /// 接收到多个手势的代理方法
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        // 判断是否是侧滑手势相关
        if gestureRecognizer == interactivePopGestureRecognizer &&
            gestureRecognizer.isKind(of: UIPanGestureRecognizer.self) {
            let pan = gestureRecognizer as! UIPanGestureRecognizer
            let point = pan.translation(in: view)
            // 如果是侧滑相关的手势，并且手势的方向是侧滑的方向就让多个手势共存
            if point.x > 0 { return true }
        }
        return false
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}
